package com.graphics.element;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: clm
 * @Date: 2018-10-27
 */

public abstract class AbsSceneNode {
    public static class RootNode extends AbsSceneNode{
        public RootNode(String name) {
            super(name);
            mVisible = false;
        }

        @Override
        public void render(float exTime) {

        }

        @Override
        public void update(float exTime) {
            mVisible = false;
        }
    }
    //当前节点子节点
    public List<AbsSceneNode> mSceneNodes;
    //当前节点父节点
    protected AbsSceneNode mParent;
    //当前节点名称(唯一标识符)
    protected String mName;
    //当前节点是否可视
    protected boolean mVisible;
    public AbsSceneNode(String name){
        mName = name;
        mVisible = true;
        mSceneNodes = new ArrayList<>();
    }
    public final void addChild(AbsSceneNode sceneNode){
        sceneNode.mParent = this;
        mSceneNodes.add(sceneNode);
    }
    public final void removeChild(AbsSceneNode sceneNode){
        if(mSceneNodes.contains(sceneNode)){
            sceneNode.mParent = null;
            mSceneNodes.remove(sceneNode);
        }
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof AbsSceneNode))return false;
        return mName.equals(((AbsSceneNode)obj).mName);
    }

    public final void renderStart(float exTime){
        if(mSceneNodes != null && mSceneNodes.size() > 0){
            for(AbsSceneNode sceneNode : mSceneNodes){
                if(sceneNode.mVisible){
                    sceneNode.render(exTime);
                    //渲染当前节点子节点
                    sceneNode.renderStart(exTime);
                }
            }
        }
    }

    public void render(float exTime){

    };

    public final void updateStart(float exTime){
        if(mSceneNodes != null && mSceneNodes.size() > 0){
            for(AbsSceneNode sceneNode : mSceneNodes){
                sceneNode.mVisible = true;          //重新标记为true
                sceneNode.update(exTime);
                //渲染当前节点子节点
                sceneNode.updateStart(exTime);
            }
        }
    }

    public void update(float exTime){

    };
}
